package com.sccc.bizImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.opensymphony.xwork2.ActionContext;
import com.sccc.Database.DatabaseCon;
import com.sccc.biz.IadminManeger;
import com.sccc.entity.Admin;
import com.sccc.entity.User;

public class AdminManeger implements IadminManeger {
	private DatabaseCon conn;//数据库连接
	private PreparedStatement ps;//预处理
	private ResultSet rs;//结果集
	private Connection connection;
	private User users;//声明一个Users对象
	List<User> allusers;//声明一个返回所有用户的结果集
	public AdminManeger(){
		this.conn = new DatabaseCon();
		this.connection = conn.getConnection();
		this.rs = null;
		this.ps = null;
	}

	@Override
	public boolean Userlogin(Admin user) {
		// 管理员登录方法
		String sql = "SELECT adminname,adminpasswd,name FROM admin WHERE adminname=? and adminpasswd=?";
		
		try {
			
			ps = (PreparedStatement) connection.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPasswd());

			   rs = ps.executeQuery();
			while(rs.next()){
				
				user.setName(rs.getString("name"));
				return true;
				
			}

            //关闭连接
			conn.closeResultSet(rs);
			conn.closeStatement(ps);
            conn.closeConnection(connection);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public List<User> All() {
		//查询并且显示所有用户方法
		        
				String sql = "SELECT * FROM user";
				allusers = new ArrayList<User>();
				
				try {
					//预处理
					ps = (PreparedStatement) connection.prepareStatement(sql);
					
					//执行查询。
					 rs = ps.executeQuery();
					 
		            while(rs.next()){
		            	users = new User();
		            	users.setId(rs.getInt("id"));
		            	users.setUsername(rs.getString("username"));
		            	users.setSex(rs.getString("sex"));
		            	users.setPasswd(rs.getString("passwd"));
		            	users.setJob(rs.getString("job"));
		            	users.setEmail(rs.getString("email"));
		            	allusers.add(users);//将查询到的所有商品添加到集合中
		            	
		            }
					//关闭查询
		            conn.closeResultSet(rs);
					conn.closeStatement(ps);
					conn.closeConnection(connection);
					
				   } catch (SQLException e) {
					e.printStackTrace();
				}
				return allusers;
	}

	@Override
	public void Add(User users) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void Del(String id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User findOneAll(int id) {
		User user =new User();	
		String sql = "select * from user where id =?;";
		try {
			//预处理
			ps = (PreparedStatement) connection.prepareStatement(sql);
			ps.setInt(1, id);
			//执行查询。
			rs = ps.executeQuery();
				
			while(rs.next()){
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setSex(rs.getString("sex"));
				user.setPasswd(rs.getString("passwd"));
				user.setJob(rs.getString("job"));
				user.setEmail(rs.getString("email"));
			}
			
			    conn.closeResultSet(rs);
				conn.closeStatement(ps);
				conn.closeConnection(connection);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
		
		return user;
	}

	@Override
	public void Up(User users) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int SQLall() {

		//统计会员总人数
		int sqlalluser = 0;
		String sql = "select count(*) aLLSQL from user";
		try {
			//预处理
			ps = (PreparedStatement) connection.prepareStatement(sql);
			
			//执行查询。
			 rs = ps.executeQuery();
			while(rs.next()){
				  sqlalluser = rs.getInt("aLLSQL");
			} 
			
			conn.closeStatement(ps);
			conn.closeConnection(connection);
				
		   } catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		return sqlalluser;
	}

}
